package com.jb.web.bean.custom.renderer;

import com.jb.faces.component.UIComponent;
import com.jb.map.domain.Address;
import com.jb.web.application.HtmlConstants;
import com.jb.web.bean.custom.GeoMap;
import com.jb.web.context.WebContext;
import com.jb.web.renderer.DefaultRenderer;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class GeoMapRenderer extends DefaultRenderer {
    public StringBuffer render(WebContext ctx, UIComponent itemToRender) {
        GeoMap map = (GeoMap) itemToRender;
        ctx.includeScript(HtmlConstants.GEOMAP);

        StringBuffer html = new StringBuffer();
        html.append("<div ").append(addTagProperty("style", map.getStyle(ctx), null))
        .append(addTagProperty("class", map.getStyleClass(), "jbGeoMap"))
        .append(getEditScript(ctx, map)).append(" >");
        html.append("</div>");
        
        Object addresses=null;
        String value = map.getModel();
        
        if(value!=null && value.length()>0)
        {
            String modelExpress ="${"+value+".addresses}";
            addresses = evaluateObject(ctx, map, modelExpress);
        }

        if(!(addresses instanceof Collection)) {
            ArrayList list = new ArrayList();
            list.add(map);
            addresses = list;
        }

        StringBuffer sb = new StringBuffer();
        if(addresses instanceof Collection) {
            Iterator iter = ((Collection)addresses).iterator();
            StringBuffer  str= new StringBuffer("{\"addresses\":[");
            while(iter.hasNext()) {
                Object address = iter.next();
                if(address instanceof Address) {
                    Address addr = (Address) address;
                    str.append("{");
                    if(addr.getLatitude()!=-1d)
                    {
                        str.append("\"latitude\":\""+addr.getLatitude()+"\",");
                        str.append("\"longitude\":\""+addr.getLongitude()+"\"");
                    }else {
                        str.append("\"postalCode\":\""+addr.getPostalCode()+"\",");
                        str.append("\"country\":\""+addr.getCountry()+"\",");
                        str.append("\"street\":\""+addr.getStreetLine1()+"\",");
                        str.append("\"address\":\""+addr.toString()+"\",");
                        str.append("\"city\":\""+addr.getCity()+"\"");
                    }
                    if(addr.getObject()!=null)
                            str.append(", \"object\":\""+addr.getObject()+"\"");
                    str.append("}");
                    if(iter.hasNext())
                        str.append(",");
                }
            }
            
            str.append("]}");

            sb.append("JB.MapTool.loadJSON('")
                .append(map.getClientId()).append("',"+str.toString())
                .append(", {mapControl:'").append(map.getMapControl())
                .append("', hierarchicalTypeControl:").append( map.isHierarchicalTypeControl())
                .append(", overviewControl:").append( map.isOverviewControl())
                .append(", typeControl:").append( map.isTypeControl())
                .append(", scale:").append( map.getScale())
                .append(", infoWindow:'")
                .append( map.getInfoWindow()).append("'");
                if(value!=null && value.length()!=0)
                    sb.append(", listId:'").append(value).append("'");
                sb.append("});");                
        }else
        {
            sb.append("JB.MapTool.load('")
            .append(map.getClientId()).append("',")
                .append(map.getLatitude()).append(",")
                .append(map.getLongitude())
                .append(", {mapControl:'")
                .append(map.getMapControl())
                .append("', typeControl:").append( map.isTypeControl())
                .append(", hierarchicalTypeControl:").append( map.isHierarchicalTypeControl())
                .append(", overviewControl:").append( map.isOverviewControl())
                .append(", infoWindow:'").append( map.getInfoWindow())
                .append("', scale:").append( map.getScale()).append("});");  
        }

        ctx.addOnLoadStmt(sb.toString());
        
        html.append("<script>");
        html.append(sb);
        html.append("</script>");
        return html;
    }
    
}
